#include "stdafx.h"
#include "caRMemAllocater.h"

caCRMemAllocaterByAPI::caCRMemAllocaterByAPI(HANDLE proc,const std::size_t &sInitialMemoryPoolSize)
{
	m_proc=proc;
	if(m_proc!=NULL)
	{
		if(m_proc)
		{
			AllocateMemory(sInitialMemoryPoolSize) ;
		}
	}
}

caCRMemAllocaterByAPI::~caCRMemAllocaterByAPI()
{
	FreeAllAllocatedMemory() ;
	DeallocateAllChunks() ;

	// Check for possible Memory-Leaks...
	assert((m_uiObjectCount == 0) && "WARNING : Memory-Leak : You have not freed all allocated Memory") ;
}

void* caCRMemAllocaterByAPI::MALLOC(std::size_t _Size)
{
	try
	{
		if(m_proc)
		{
			return ::VirtualAllocEx(m_proc, NULL, _Size, MEM_COMMIT,PAGE_EXECUTE_READWRITE);				
		}
		return NULL;
	}
	catch (...)
	{
		return NULL;
	}	
}

void caCRMemAllocaterByAPI::FREE(void* _Memory)
{
	try
	{
		if(m_proc)
		{
			::VirtualFreeEx(m_proc, _Memory, 0, MEM_RELEASE);				
		}
	}
	catch (...)
	{
		return;
	}
}